<template>
  <div id="main">
    <h2>学生临床信息</h2>
    <div class="head">
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin-right:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="studentName"
          label="姓名"
        />

        <el-table-column
          align="center"
          prop="studentAge"
          :formatter="formatAge"
          label="年龄"
        />
        <el-table-column
          align="center"
          prop="studentSex"
          :formatter="formatSex"
          label="性别"
        />
        <el-table-column
          align="center"
          prop="studentJoinTime"
          label="训练时长"
        />
        <el-table-column
          align="center"
          prop="studentTreatCurrentDiagnosis"
          label="临床诊断"
        />
        <el-table-column
          align="center"
          prop="studentTreatPrincipalTeacherName"
          label="主责老师名称"
        />
        <el-table-column
          align="center"
          prop="studentTreatPrincipalTeacherName"
          label="教学主管名称"
        />
        <el-table-column
          align="center"
          prop="studentTreatMedicalDirectorName"
          label="医疗主管名称"
        />
        <el-table-column
          align="center"
          prop="studentTreatPostTime"
          label="录入时间"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改孩子', 2, Scope.row.studentId)">查看</el-button>

          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- --------------------------------------------------------------------------------------------------------- -->

    <el-dialog title="" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="student" :model="student" label-position="top" :rules="rules">

        <el-form-item prop="studentTreatAttention" label="训练注意事项" :label-width="formLabelWidth">
          <!-- <el-input v-model="student.studentTreatAttention" autocomplete="off" /> -->
          {{ student.studentTreatAttention }}
        </el-form-item>

        <el-form-item prop="studentTreatTrainingPrescription " label="训练处方" :label-width="formLabelWidth">
          <!-- <el-input v-model="student.studentTreatTrainingPrescription" autocomplete="off" /> -->
          {{ student.studentTreatTrainingPrescription }}
        </el-form-item>

        <el-form-item v-if="this.jurisdiction =='责任教师'" prop="studentTreatAssessmentInformation " label="医学评估" :label-width="formLabelWidth">
          <!-- <el-input v-model="student.studentTreatTrainingPrescription" autocomplete="off" /> -->
          {{ student.studentTreatAssessmentInformation }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addChild, deleteChild, updateChild, getAllChild, getChildById, getAllParent } from '@/api/child'
import store from '@/store/index'
export default {
  data() {
    return {
      url: process.env.VUE_APP_BASE_API + 'file/',
      form: [],
      formRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[34578]\d{9}$/,
            message: '目前只支持中国大陆的手机号码'
          }
        ]
      },
      sex: [
        {
          value: 0,
          label: '女'
        },
        {
          value: 1,
          label: '男'
        }
      ],
      father: [],
      mother: [],
      String: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      imageUrl: '',
      tableList: [],
      searchTableInfo: '',
      train: {},
      train_id: [],
      trainingInstitutionRecordId: '',
      listLoading: false,
      dialogFormVisible: false,
      is_add_father: false,
      is_add_mother: false,
      is_add_train: false,
      this_stuid: '',
      jurisdiction: '',
      editType: 1, // 1 添加 2 修改
      student: [],
      formLabelWidth: '200px'
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.jurisdiction = store.getters.roles[0]
    this.fetchData()
    this.getparent()
  },
  methods: {

    formatSex: function(row, column, cellValue) {
      if (cellValue === '1') {
        return '女'
      } else if (cellValue === '0') {
        return '男'
      }
    },

    formatAge: function(row, column, cellValue) {
      if (cellValue === -1) {
        return '未填写'
      } else {
        return cellValue
      }
    },
    getparent: function() {
      getAllParent(1).then(resp => {
        this.father = resp.obj
      })
      getAllParent(0).then(r => {
        this.mother = r.obj
      })
    },
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {

    },
    fetchData() {
      this.listLoading = true
      getAllChild().then(data => {
        this.tableList = data.obj
        this.listLoading = false
      })
      getAllParent(1).then(f => {
        this.father = f.obj
      })
      getAllParent(0).then(m => {
        this.mother = m.obj
      })
    },

    handleOpen(title, type, id) {
      this.dialogFormVisible = true
      for (var i = 0; i < this.tableList.length; i++) {
        if (this.tableList[i].studentId === id) {
          this.student.studentTreatAttention = this.tableList[i].studentTreatAttention
          this.student.studentTreatTrainingPrescription = this.tableList[i].studentTreatTrainingPrescription
          this.student.studentTreatAssessmentInformation = this.tableList[i].studentTreatAssessmentInformation
        }
      }
    },

    handleClose() {
      this.dialogFormVisible = false
      this.train_id = []
      this.form = []
      this.student = []
      this.imageUrl = ''
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
